package com.jiyun.twostudio.service.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.twostudio.fegin.StudioFegin;
import com.jiyun.twostudio.service.StudioService;
import com.usian.model.atri.pojos.ApArticle;
import com.usian.model.behavior.ApLikesBehavior;
import com.usian.model.comment.*;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentLike;
import com.usian.model.comment.pojos.ApCommentVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudioServiceipl implements StudioService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StudioFegin studioFegin;

//文章评论
    @Override
    public ResponseResult pinglun(CommentSaveDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不匹配");
        }
////先判断登录用户
//        ApUser user = AppThreadLocalUtils.getUser();
//        if (user==null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户未登录");
//        }
       Integer userid=1102;

//根据前台文章id  去查询文章表的信息  进行文章  和 评论 保存
        ApArticle apArticle= studioFegin.wzcha(dto.getArticleId());
        if (apArticle==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文章未查询出来");
        }
        ApComment apComment=new ApComment();
        apComment.setAuthorId(apArticle.getAuthorId());
        apComment.setAuthorName(apArticle.getAuthorName());
        apComment.setContent(dto.getContent());
        apComment.setCreatedTime(new Date());
        apComment.setChannelId(apArticle.getChannelId());
        apComment.setUpdatedTime(new Date());
        apComment.setAddress("1");
        apComment.setFlag(5);
        apComment.setChannelId(1111);
        apComment.setImage("211");
        apComment.setReply(10);
        apComment.setLikes(10);
//
        mongoTemplate.insert(apComment);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

//    点赞回复
    @Override
    public ResponseResult dianzan(CommentLikeDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不匹配");
        }

        ApComment byId = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
       if (dto.getOperation()==0 && byId!=null){
           Query entryId = Query.query(Criteria.where("_id").is(dto.getCommentId()));
           Update likes = new Update().set("likes",byId.getLikes()+1);
           mongoTemplate.updateMulti(entryId,likes,ApComment.class);
           ApCommentLike apCommentLike=new ApCommentLike();
           apCommentLike.setOperation(dto.getOperation());
           apCommentLike.setAuthorId(1102);
           apCommentLike.setCreatedTime(new Date());
           apCommentLike.setCommentId(byId.getId());
           mongoTemplate.insert(apCommentLike);
       }else if (dto.getOperation()==1 ){
           if (byId.getLikes()>0){
               Query query = Query.query(Criteria.where("authorId").is("1102").and("_id").is(dto.getCommentId()));
               mongoTemplate.remove(query,ApCommentLike.class);
               Query entryId = Query.query(Criteria.where("_id").is(dto.getCommentId()));
               Update likes1 = new Update().set("likes",byId.getLikes()-1);
               mongoTemplate.updateMulti(entryId,likes1,ApComment.class);
               mongoTemplate.remove(ApComment.class);
           }


       }



        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult pinglunlist(CommentSaveDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不匹配");
        }
        ApComment apComment=new ApComment();
        apComment.setAuthorId(dto.getArticleId());
        List<ApComment> all = mongoTemplate.findAll(ApComment.class);
       List<ApCommentVo>  volist=new ArrayList<>();
        ApCommentVo apCommentVo=new ApCommentVo();
        for (ApComment comment : all) {
            ApCommentLike apCommentLike=new ApCommentLike();
            apCommentLike.setCommentId(comment.getId());
            List<ApCommentLike> byId = mongoTemplate.findAll( ApCommentLike.class);
            for (ApCommentLike commentLike : byId) {
                apCommentVo.setOperation(commentLike.getOperation());
            }
            apCommentVo.setApComment(comment);
            volist.add(apCommentVo);
        }
        return ResponseResult.okResult(volist);
    }


//
    @Override
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {
        return null;
    }

    @Override
    public ResponseResult saveCommentRepayLike(CommentRepayLikeDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不匹配");
        }
        ApCommentRepay byId = mongoTemplate.findById(dto.getCommentRepayId(), ApCommentRepay.class);
        if (byId==null && dto.getOperation()==0){
            Query query=Query.query(Criteria.where("_id").is(dto.getCommentRepayId()));
            Update likes=new Update().set("likes",byId.getLikes()+1);
            mongoTemplate.updateMulti(query,likes,ApCommentRepay.class);
            ApCommentRepayLike apCommentRepayLike=new ApCommentRepayLike();
            apCommentRepayLike.setCommentRepayId(dto.getCommentRepayId());
            apCommentRepayLike.setOperation(0);
            apCommentRepayLike.setAuthorId(1102);
            mongoTemplate.insert(apCommentRepayLike);
        }



        return null;
    }

//    二级评论  先添加评论 然后 修改主表中 评论的回复次数
    @Override
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApCommentRepay apCommentRepay=new ApCommentRepay();
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setUpdatedTime(new Date());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setLikes(0);
        apCommentRepay.setAuthorId(1102);
        apCommentRepay.setAddress("河北");
        mongoTemplate.insert(apCommentRepay);
        ApComment byId = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        Query id = Query.query(Criteria.where("_id").is(dto.getCommentId()));
        Update reply = new Update().set("reply",byId.getReply()+1);
        mongoTemplate.updateMulti(id,reply,ApComment.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
